package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.bo.UserInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Classname LocationAccessAttrHandler
 * @Description 处理大带宽 接入地（大带宽
 * @Author zhang.song
 * @Date 2021-03-02 14:47
 */
public class LocationAccessAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Resource
    private IUnicomRegionService unicomRegionService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    private static final String DATA_CORRECTION = "1080";//资料修正


    /**装机地址校验放到订单预览规则中心校验*/
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
    }

    /**
     * 装机地址 省市默认值：操作人省市
     */
    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        // 订购、核查、资料修正 操作，初始默认值
        List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_ORDER, LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)
            || (KiteStringUtils.isEqual(data.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_MODIFY) && KiteListUtils.isNotEmpty(serviceOfferIdList) && serviceOfferIdList.contains(DATA_CORRECTION))) {
            String filedName = attr.getFieldName();
            String productId = inst.getSpecId();
            List<String> filterProdId = this.getFilterProdId();
            if(filterProdId.contains(productId) && KiteStringUtils.equalsAny(filedName, "kit0000224", "2_0000105")) {
                // 省际、省内，Z端没有默认值
                return;
            }

            String value = attr.getValue();
            if (KiteStringUtils.isEmpty(value)) {
                UserInfo userInfo = ContextUtil.getLoginInfo().getUserInfo();
                if(userInfo != null) {
                    String provinceId = userInfo.getPostProvinceNbr();
                    String cityId = userInfo.getPostLanId();
                    if(KiteStringUtils.isNotEmpty(provinceId)) {
                        super.set(attr, inst, provinceId + (KiteStringUtils.isEmpty(cityId)?"":(","+cityId)), data);
                    }
                }
            }

        }
        super.after(attr, inst, data);
    }

    /**
     * 获取页面展示名称
     *
     * @param attr 实例属性入参
     * @param inst 当前操作的实例对象
     * @param value 设置的值
     * @param data 属性处理入参，可以根据需要传入需要的业务数据
     * @return 属性页面展示名称
     */
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        String[] regionCodes = KiteStringUtils.split(value, SymbolConsts.COMMA);
        List<String> regionNames = Arrays.stream(regionCodes).map(s -> {
            UnicomRegion region = unicomRegionService.getRegion(s);
            return Objects.nonNull(region) ? region.getRegionName() : "";
        }).collect(Collectors.toList());
        return KiteStringUtils.join(regionNames, SymbolConsts.HYPHEN);
    }

    private List<String> getFilterProdId() {
        List<String> filterProdIds = new ArrayList<>();
        Optional.ofNullable(ProdCateUtil.getProdIdByCode(ProdCateUtil.YTW_CODE)).ifPresent(filterProdIds::addAll);
        Optional.ofNullable(ProdCateUtil.getProdIdByPCode("SJCT_PROD_ID", ProdCateUtil.SDH_CODE)).ifPresent(filterProdIds::addAll);
        Optional.ofNullable(ProdCateUtil.getProdIdByPCode("SNCT_PROD_ID", ProdCateUtil.SDH_CODE)).ifPresent(filterProdIds::addAll);
        Optional.ofNullable(ProdCateUtil.getProdIdByCode(ProdCateUtil.MV_CODE)).ifPresent(filterProdIds::addAll);
        return filterProdIds;
    }
}
